package com.it.server.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.it.common.context.BaseContext;
import com.it.common.exception.OrderBusinessException;
import com.it.common.result.PageResult;
import com.it.common.result.Result;
import com.it.common.utils.WeChatPayUtil;
import com.it.pojo.DTO.OrderDTO;
import com.it.pojo.DTO.OrderPaymentDTO;
import com.it.pojo.Entity.Users;
import com.it.pojo.VO.OrderPaymentVO;
import com.it.pojo.VO.OrderSubmitVO;
import com.it.pojo.VO.OrdersVO;
import com.it.server.service.OrderService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/user/order")
@Tag(name = "订单接口")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    @Operation(summary="提交订单")
    public Result<OrderSubmitVO> submit(@RequestBody OrderDTO orderDTO) {
        OrderSubmitVO orderSubmitVO =orderService.submit(orderDTO);
        return Result.success(orderSubmitVO);
    }
    @PutMapping("/payment")
    @Operation(summary="订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrderPaymentDTO orderPaymentDTO) throws Exception {
        OrderPaymentVO orderPaymentVO = orderService.payment(orderPaymentDTO);
        return Result.success(orderPaymentVO);
    }
    @PutMapping("/cancel")
    @Operation(summary="取消订单")
    public Result cancel(@PathVariable("id") Integer  id)throws  Exception {
        orderService.cancel(id);
        return Result.success();
    }
    /**
     * 历史订单查询
     *
     * @param page
     * @param pageSize
     * @param status   订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     * @return
     */
    @GetMapping("/historyOrders")
    @Operation(summary="历史订单查询")
    public Result<PageResult> page(int page, int pageSize, Integer status) {
        PageResult pageResult = orderService.pageQuery(page, pageSize, status);
        return Result.success(pageResult);
    }
    @GetMapping("/detail/{id}")
    @Operation(summary="订单详情")
    public Result<OrdersVO> detail(@PathVariable Integer id) {
        OrdersVO orderVO = orderService.getOrderDetail(id);
        return Result.success(orderVO);
    }
}
